package com.cskaoyan.market.controller;

import com.cskaoyan.market.service.WxCartService;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.CheckOutVo;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/wx/cart")
public class WxCartController {

    @Autowired
    WxCartService cartService;
    @Autowired
    SecurityManager securityManager;

    //显示目前购物车中商品的数量
    @GetMapping("goodscount")
    public Object goodsCount() {
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("id");

        if (userId == null) {
            userId = 0;
        }
        Integer count = cartService.goodsCount(userId);
        return ResponseUtil.ok(count);
    }

    //在商品界面点击加入购物车增加购物车商品
    @PostMapping("add")
    public Object add(@RequestBody String goodsMsg) {

        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("id");

        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        Integer goodsId = JacksonUtil.parseInteger(goodsMsg, "goodsId");
        Integer number = JacksonUtil.parseInteger(goodsMsg, "number");
        Integer productId = JacksonUtil.parseInteger(goodsMsg, "productId");

        Object result = cartService.add(goodsId, number, productId, userId);

        return ResponseUtil.ok(result);

    }

    //显示购物车页面
    @GetMapping("index")
    public Object index() {

        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("id");
        Map<String, Object> cartList = cartService.index(userId);
        return ResponseUtil.ok(cartList);
    }

    @PostMapping("update")
    public Object update(@RequestBody String goodsMsg) {
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("id");
        Integer id = JacksonUtil.parseInteger(goodsMsg, "id");
        Integer goodsId = JacksonUtil.parseInteger(goodsMsg, "goodsId");
        short number = JacksonUtil.parseShort(goodsMsg, "number");
        Integer productId = JacksonUtil.parseInteger(goodsMsg, "productId");

        int result = cartService.update(goodsId, number, productId, id, userId);
        if (result == 200) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.badArgument();
    }

    //选中
    @PostMapping("checked")
    public Object checked(@RequestBody String checkedmsg) {

        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("id");

        Integer isChecked = JacksonUtil.parseInteger(checkedmsg, "isChecked");
        List<Integer> productIds = JacksonUtil.parseIntegerList(checkedmsg, "productIds");
        Object result = cartService.checked(isChecked, productIds, userId);

        return ResponseUtil.ok(result);
    }

    //立即下单
    @PostMapping("fastadd")
    public Object fastAdd(@RequestBody String goodsMsg) {

        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("id");

        Integer goodsId = JacksonUtil.parseInteger(goodsMsg, "goodsId");
        short number = JacksonUtil.parseShort(goodsMsg, "number");
        Integer productId = JacksonUtil.parseInteger(goodsMsg, "productId");

        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        int result = cartService.fastAdd(goodsId, number, productId, userId);

        if (result == 200) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.badArgument();
    }

    //下单
    @GetMapping("checkout")
    public Object WxCheckOut(Integer addressId, Integer couponId, Integer userCouponId) {
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer id = (Integer) session.getAttribute("id");

        CheckOutVo checkout = cartService.getCheckoutList(addressId, couponId, userCouponId, id);
        return ResponseUtil.ok(checkout);
    }

    //删除购物车内商品
    @PostMapping("delete")
    public Object delete(@RequestBody String deletemsg) {

        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("id");

        List<Integer> productIds = JacksonUtil.parseIntegerList(deletemsg, "productIds");
        Object result = cartService.delete(productIds, userId);

        return ResponseUtil.ok(result);
    }
}
